package com.nami.conver.domain.pchat.service;


import com.nami.conver.domain.disruptor.TaskDisruptor;
import com.nami.conver.domain.repository.CacheConverRepository;
import com.nami.conver.domain.repository.ConverTaskRepository;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Component;



@Slf4j
@Component
@RequiredArgsConstructor
public class ConverTaskService {

    private final ConverTaskRepository converTaskRepository;
    private final CacheConverRepository cacheConverRepository;
    private final RedissonClient redisson;

    private TaskDisruptor taskDisruptor;

    @PostConstruct
    public void init() {
        // 初始化 Disruptor
        taskDisruptor = new TaskDisruptor(converTaskRepository, cacheConverRepository, redisson);
    }

    @PreDestroy
    public void destroy() {
        // 关闭 Disruptor
        taskDisruptor.shutdown();
        log.info("[disruptor] shutdown service.");
    }

    /**
     * 异步会话同步任务
     *
     * @param taskKey
     */
    public void addAsyncWriteConverTask(String taskKey) {
        taskDisruptor.add(taskKey);
    }

}
